{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Alignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.table.*\n",
    "import com.twosigma.beakerx.fileloader.CSV\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "\n",
    "// 1 setAlignmentProviderForColumn defaults\n",
    "def colNames = [\"string column\", \"integer column\", \"double column\",\"time column\", \"boolean column\"]\n",
    "def row1 = [\"0\", 0, 0, 0, 0]\n",
    "def row2 = [\"June\", 22, 33, 1391000000, true]\n",
    "def row3 = [\"July\", 200, 300, 1491000000, false]\n",
    "def row4 = [\"August\", 222, 333, 1591000000, true]\n",
    "table1 = new TableDisplay([row1, row2, row3, row4], colNames, ['string', 'integer', 'double', 'time', 'boolean'])\n",
    "table1.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "table1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 1\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell1_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 1_2 setAlignmentProviderForColumn\n",
    "def colNames = [\"string column\", \"integer column\", \"double column\",\"time column\", \"boolean column\"]\n",
    "def row1 = [\"0\", 0, 0, 0, 0]\n",
    "def row2 = [\"June\", 22, 33, 1391000000, true]\n",
    "def row3 = [\"July\", 200, 300, 1491000000, false]\n",
    "def row4 = [\"August\", 222, 333, 1591000000, true]\n",
    "table1_2 = new TableDisplay([row1, row2, row3, row4], colNames, ['string', 'integer', 'double', 'time', 'boolean'])\n",
    "table1_2.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "table1_2.setAlignmentProviderForColumn(colNames[0], TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table1_2.setAlignmentProviderForColumn(colNames[1], TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table1_2.setAlignmentProviderForColumn(colNames[2], TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table1_2.setAlignmentProviderForColumn(colNames[3], TableDisplayAlignmentProvider.LEFT_ALIGNMENT)\n",
    "table1_2.setAlignmentProviderForColumn(colNames[4], TableDisplayAlignmentProvider.RIGHT_ALIGNMENT)\n",
    "table1_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 1_2\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell1_case2.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 2 setAlignmentProviderForType\n",
    "def colNames = [\"string column\", \"integer column\", \"double column\",\"time column\", \"boolean column\"]\n",
    "def row1 = [\"0\", 0, 0, 0, 0]\n",
    "def row2 = [\"June\", 22, 33, 1391000000, true]\n",
    "def row3 = [\"July\", 200, 300, 1491000000, false]\n",
    "def row4 = [\"August\", 222, 333, 1591000000, true]\n",
    "table2 = new TableDisplay([row1, row2, row3, row4], colNames, ['string', 'integer', 'double', 'time', 'boolean'])\n",
    "table2.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "table2.setAlignmentProviderForType(ColumnType.String, TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table2.setAlignmentProviderForType(ColumnType.Integer, TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table2.setAlignmentProviderForType(ColumnType.Double, TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "table2.setAlignmentProviderForType(ColumnType.Time, TableDisplayAlignmentProvider.LEFT_ALIGNMENT)\n",
    "table2.setAlignmentProviderForType(ColumnType.Boolean, TableDisplayAlignmentProvider.RIGHT_ALIGNMENT)\n",
    "table2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 2\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell2_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bar Charts Renderer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 3\n",
    "table3 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table3.setRendererForType(ColumnType.Double, TableDisplayCellRenderer.getDataBarsRenderer(true))\n",
    "// use the false parameter to hide value\n",
    "table3.setRendererForColumn(\"y10\", TableDisplayCellRenderer.getDataBarsRenderer(false))\n",
    "table3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 3\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell3_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Formatting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 4\n",
    "table4 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table4.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "table4.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(2,3))\n",
    "table4.setStringFormatForColumn(\"m3\", TableDisplayStringFormat.getDecimalFormat(0, 0))\n",
    "table4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 4\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell4_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HTML format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 5\n",
    "table5 = new TableDisplay([x: '<em style=\"color:red\">italic red</em>',\n",
    "                      y: '<b style=\"color:blue\">bold blue</b>',\n",
    "                      z: 'multiline strings<br/>work fine too']);\n",
    "table5.setStringFormatForColumn(\"Value\", TableDisplayStringFormat.getHTMLFormat())\n",
    "table5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 5\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell5_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Column Visibility and Placement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 6\n",
    "table6 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "//freeze a column\n",
    "table6.setColumnFrozen(\"y1\", true)\n",
    "//hide a column\n",
    "table6.setColumnVisible(\"y30\", false)\n",
    "table6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 6\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell6_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 7\n",
    "table7 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "// Columns in the list will be shown in the provided order. Columns not in the list will be hidden.\n",
    "table7.setColumnOrder([\"m3\", \"y1\", \"y10\", \"time\", \"y2\"])\n",
    "table7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 7\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell7_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HeatmapHighlighter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 8 highlighter for row\n",
    "table8 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table8.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m3\", TableDisplayCellHighlighter.FULL_ROW))\n",
    "table8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 8\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell8_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 9 highlighter for column\n",
    "table9 = new TableDisplay(new CSV().read(\"../../resources/data/interest-rates-small.csv\"))\n",
    "table9.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m6\", TableDisplayCellHighlighter.SINGLE_COLUMN, 6, 8, Color.BLACK, Color.PINK))\n",
    "table9.addCellHighlighter(new ThreeColorHeatmapHighlighter(\"y1\", TableDisplayCellHighlighter.SINGLE_COLUMN, 4, 6, 8, new Color(247,106,106), new Color(239,218,82), new Color(100,189,122)))\n",
    "table9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 9\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell9_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 9_2\n",
    "table9.removeAllCellHighlighters()\n",
    "table9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 9_2\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell9_case2.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 9_3 set HeatmapHighlighter for type\n",
    "def colNames = [\"xxx column\", \"integer column\", \"double column\", \"number column\"]\n",
    "def row1 = [6, 6, 0.5, 6]\n",
    "def row2 = [3, 3, 2.0, 3]\n",
    "def row3 = [2, 2, 3.0, 2]\n",
    "def row4 = [0, 0, 6.0, 0]\n",
    "table93 = new TableDisplay([row1, row2, row3, row4], colNames, ['xxx type', 'integer', 'double', 'number'])\n",
    "table93.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(colNames[0], TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table93.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(colNames[1], TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table93.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(colNames[2], TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table93.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(colNames[3], TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table93"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 9_3\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell9_case3.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## UniqueEntriesHighlighter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 10\n",
    "table10 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table10.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter(\"m3\"))\n",
    "table10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 10\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell10_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 11\n",
    "table11 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table11.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter(\"y30\", TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table11.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter(\"m6\", TableDisplayCellHighlighter.SINGLE_COLUMN))\n",
    "table11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 11\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell11_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FontSize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 12\n",
    "table12 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table12.setDataFontSize(10)\n",
    "table12.setHeaderFontSize(16)\n",
    "table12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 12\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell12_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 13\n",
    "table13 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "table13.setHeadersVertical(true)\n",
    "table13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 13\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell13_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ColorProvider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 14\n",
    "def mapListColorProvider = [\n",
    " [firstCol:1, secondCol:2, thirdCol:3],\n",
    " [firstCol:4, secondCol:5, thirdCol:6],\n",
    " [firstCol:9, secondCol:8, thirdCol:9]\n",
    "]\n",
    "table14 = new TableDisplay(mapListColorProvider)\n",
    "\n",
    "colors = [\n",
    "    [Color.LIGHT_GRAY, Color.GRAY, Color.RED],\n",
    "    [Color.DARK_GREEN, Color.ORANGE, Color.RED],\n",
    "    [Color.MAGENTA, Color.BLUE, Color.BLACK]\n",
    "]\n",
    "\n",
    "table14.setFontColorProvider { row, col, td ->\n",
    " colors[row][col]\n",
    "}\n",
    "table14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 14\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell14_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ToolTip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 15\n",
    "table15 = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "\n",
    "table15.setToolTip { row, col, display ->\n",
    " \"The value is: \" + display.values[row][col]\n",
    "}\n",
    "table15"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 16_1\n",
    "table16_1 = new TableDisplay([['Peacock': '../../resources/img/butterfly1.jpg',\n",
    "                        'Morpho': '../../resources/img/butterfly2.jpg',\n",
    "                        'Swallowtail': '../../resources/img/butterfly3.jpg']]);\n",
    "table16_1.setStringFormatForColumn('Peacock', TableDisplayStringFormat.getImageFormat());\n",
    "table16_1.setStringFormatForColumn('Morpho', TableDisplayStringFormat.getImageFormat());\n",
    "table16_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 16_1\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell16_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 16_2\n",
    "table16_2 = new TableDisplay(['up': '',\n",
    "                        'down': '']);\n",
    "table16_2.setStringFormatForColumn(\"Value\", TableDisplayStringFormat.getImageFormat())\n",
    "table16_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 16_2\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell16_case2.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Seamless Update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// 19\n",
    "def mapListToUpdate = [\n",
    "   [a:1, b:2, c:3],\n",
    "   [a:4, b:5, c:6],\n",
    "   [a:7, b:8, c:9]\n",
    "]\n",
    "table19 = new TableDisplay(mapListToUpdate)\n",
    "table19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// table19.values[0][0] = 99\n",
    "// table19.sendModel()\n",
    "table19.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 19\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell19_case1.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 19 after sendModel()\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell19_case2.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RowFilter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mapList20 = [\n",
    " [firstCol:1, secondCol:2, thirdCol:3],\n",
    " [firstCol:4, secondCol:8, thirdCol:6],\n",
    " [firstCol:9, secondCol:8, thirdCol:9]\n",
    "]\n",
    "table20 = new TableDisplay(mapList20)\n",
    "table20.setRowFilter { row, model ->\n",
    " model[row][1] == 8\n",
    "}\n",
    "table20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result of cell 20\n",
    "Image(\"../../resources/img/groovy/tableAPI/cell20_case1.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The length of types should be same as number of cols."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// expected result is error message\n",
    "def colNames = [\"column 1\", \"column 2\", \"column 3\"]\n",
    "def row1 = [6, 6, 0.5]\n",
    "def row2 = [3, 3, 2.0]\n",
    "table94 = new TableDisplay([row1, row2], colNames, ['integer', 'double' ])\n",
    "table94"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set global timezone"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "TableDisplay.setTimeZoneGlobally(\"Europe/London\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tableGTZ = new TableDisplay(new CSV().read('../../resources/data/interest-rates-small.csv'))\n",
    "tableGTZ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tableGTZ.setTimeZone(\"America/Los_Angeles\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.5.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
